/* Copyright [2023] <gyl> */
#include <string>
#include <vector>

#include "sql/stmt/grant_stmt.h"

#include "common/global_context.h"
#include "sql/parser/parse.h"
#include "sql/stmt/filter_stmt.h"
#include "sql/stmt/select_stmt.h"
#include "sql/stmt/utils.h"
#include "storage/db/db.h"
#include "storage/table/table.h"
#include "storage/table/table_meta.h"

RC GrantStmt::create(Db *db, const GrantSqlNode &grant, Stmt *&stmt) {
  RC rc = RC::SUCCESS;
  // 检查授权的表是否存在
  std::vector<std::string> relations = grant.relations;
  for (int i = 0; i < relations.size(); i++) {
    Table *table = db->find_table(relations[i].c_str());
    if (table == nullptr && !(relations[i] == "*" && relations.size() == 1)) {
      return RC::SCHEMA_TABLE_NOT_EXIST;
    }
  }
  stmt = new GrantStmt(grant.relations, grant.users, grant.priv);
  return RC::SUCCESS;
}
